package com.bridge.client.bridgedynamcclient.utils;

import com.bridge.client.bridgedynamcclient.controller.BaseController;
import com.bridge.client.bridgedynamcclient.controller.BridgeAddMainController;
import com.bridge.client.bridgedynamcclient.custom_compinents.CommonDialogStage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.util.Callback;

import java.io.IOException;
import java.util.Optional;

public class FXMLUtil {

    public static FXMLLoader getLoader(Class clazz,String viewResPath){
        return new FXMLLoader(clazz.getResource(viewResPath));
    }

    public static void loadView(FXMLLoader fxmlLoader){
        AnchorPane root = null;
        try {
            root = fxmlLoader.load();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Stage stage = new Stage();
        stage.setScene(new Scene(root));
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("桥梁结构动力特性");
        stage.setResizable(false);
        stage.getIcons().add(getImage("/images/icon_bridge.png"));
        Object controller = fxmlLoader.getController();
        if(controller instanceof  BaseController){
            ((BaseController) controller).setRoot(root);
        }
        stage.show();
    }

    public static void loadView(FXMLLoader fxmlLoader,Modality modality,String title){
        AnchorPane root = null;
        try {
            root = fxmlLoader.load();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Stage stage = new Stage();
        stage.setScene(new Scene(root));
        stage.initModality(modality);
        stage.setTitle(title);
        stage.setResizable(false);
        stage.getIcons().add(getImage("/images/icon_bridge.png"));
        Object controller = fxmlLoader.getController();
        if(controller instanceof  BaseController){
            ((BaseController) controller).setRoot(root);
        }
        stage.show();

    }

    public static void closeState(Scene scene){
        Stage currentState= (Stage) scene.getWindow();
        currentState.close();
    }

    public static void showAlertInfo(String message){
        Alert alert=new Alert(Alert.AlertType.INFORMATION,message, ButtonType.OK);
        alert.setHeaderText("提醒消息");
        alert.setTitle("提醒");
        alert.showAndWait();
    }

    public static Optional<ButtonType> showWarningInfo(String tipTitle,String message){
        Alert alert=new Alert(Alert.AlertType.WARNING,message, ButtonType.YES,ButtonType.NO);
        alert.setHeaderText(tipTitle);
        alert.setTitle("警告提醒");
        return alert.showAndWait();
    }



    public static String getResource(String resources) {
        return FXMLUtil.class.getResource(resources).toExternalForm();
    }

    public static Image getImage(String resources) {
        return new Image(getResource(resources));
    }

    public static ImageView getImageView(String resources, double size) {
        return getImageView(resources, size, size);
    }

    public static ImageView getImageView(String resources, double height, double width) {
        ImageView imageView = new ImageView(getResource(resources));
        imageView.setFitHeight(height);
        imageView.setFitWidth(width);
        return imageView;
    }

    public static ImageView getImageViewByUrl(String url,double height,double width){
        //需要异步 加载 true
        ImageView imageView = new ImageView(new Image(url,true));
        imageView.setFitHeight(height);
        imageView.setFitWidth(width);
        return imageView;
    }

    public static  Optional<Boolean> showCommonDialog(Window window,String title, String content) {
        CommonDialogStage commonDialogStage=new CommonDialogStage(title,content);

        commonDialogStage.initOwner(window);
        Optional<Boolean> optional = commonDialogStage.showAndWait();
        return optional;

    }
}
